# coding: utf8
'''
微博设置页面
'''

__revision__ = 0.1

from SakyaTemplate import lookup
from forms import t_form, p_form
from models import Tblog, TBProfile
import web
from sqlalchemy.exc import SQLAlchemyError

class index(object):
    def GET(self):
        blogs = web.ctx.orm.query(Tblog).order_by(Tblog.gmtCreate).limit(10)
        return lookup.get_template('t/index.htm').render(blogs = blogs)

class tblog(object):
    def GET(self):
        if not web.ctx.session.get('isAuth') or web.ctx.session.get('uid') == None:
            return web.seeother('../user/login.htm')
        username = web.ctx.session.logId
        uid = web.ctx.session.uid
        form = t_form()
        blogs = web.ctx.orm.query(Tblog).filter_by(uid = uid).order_by('gmtCreate desc').limit(10)
        count = web.ctx.orm.query(Tblog).filter_by(uid = uid).count()
        return lookup.get_template('t/tblog.htm').render(form = form, blogs = blogs)

class profile(object):
    def GET(self):
        if not web.ctx.session.get('isAuth') or web.ctx.session.get('uid') == None:
            return web.seeother('../user/login.htm')
        uid = web.ctx.session.get('uid')
        tprofile = web.ctx.orm.query(TBProfile).filter_by(uid = uid).first()
        form = p_form()
        form.uid.value = web.ctx.session.get('uid')
        if tprofile is not None:
            form.tdomain.value = tprofile.tdomain
            form.tdomain.attrs['readonly'] = 'true'
            form.type.value = tprofile.type
            form.tname.value = tprofile.tname
            form.mydesc.value = tprofile.mydesc
        return lookup.get_template('t/profile.htm').render(form = form)
class doProfile(object):
    def POST(self):
        if not web.ctx.session.get('isAuth') or web.ctx.session.get('uid') == None:
            return web.seeother('../user/login.htm')
        uid = web.ctx.session.get('uid')
        tprofile = web.ctx.orm.query(TBProfile).filter_by(uid = uid).first()

        form = p_form()
        if tprofile is not None:
            form.tdomain.attrs['readonly'] = 'true'
        if not form.validates():
            return lookup.get_template('t/profile.htm').render(form = form)
        
        tprofile_new = TBProfile(form.uid.value, form.tdomain.value, form.type.value, form.tname.value, form.mydesc.value)
        try :
            # save or update
            web.ctx.orm.add(tprofile_new)
            web.ctx.orm.flush()
        except SQLAlchemyError, e:
            return lookup.get_template('t/profile.htm').render(form = form, msg=e)
        return web.seeother('profile.htm')

class doPublish(object):
    def POST(self):
        if not web.ctx.session.get('isAuth') or web.ctx.session.get('uid') == None:
            return web.seeother('../user/login.htm')
        uid = web.ctx.session.uid
        form = t_form()
        if not form.validates():
            return lookup.get_template('t/tblog.htm').render(form = form)
        tbv = form.tblog.value
        tb = Tblog(uid, tbv)
        web.ctx.orm.add(tb)
        web.ctx.orm.flush()
        raise web.seeother('tblog.htm')

